home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / hostname / hostname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-30  |  4.2 KB  |  198 lines

  1. /* 
  2.  * hostname.c --
  3.  *
  4.  *    A very simple program that prints out the textual name of
  5.  *    the host on which it is running.
  6.  *
  7.  * Copyright 1988 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/cmds/hostname/RCS/hostname.c,v 1.6 91/11/29 16:43:21 jhh Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <option.h>
  25. #include <host.h>
  26. #include <sys.h>
  27.  
  28. /*
  29.  * This is for the sprite Proc_GetIDs call, until it is added to the unix
  30.  * library.
  31.  */
  32. #include <sprite.h>
  33. #include <status.h>
  34. #include <proc.h>
  35.  
  36. int getIDs = 0;
  37. int virtual = 0;
  38. int physical = 0;
  39. int machType = -1;
  40.  
  41. Option optionArray[] = {
  42.     {OPT_TRUE, "v", (char *)&virtual,
  43.      "Print the name of the host on which the process is effectively executing (its 'home node' -- DEFAULT)."},
  44.     {OPT_TRUE, "p", (char *)&physical,
  45.      "Print the name of the host on which the process is physically executing ."},
  46.     {OPT_TRUE, "i", (char *)&getIDs,
  47.      "Print hostIDs instead of host names."},
  48.     {OPT_TRUE, "type", (char *)&machType,
  49.      "Print machine type for host."},
  50. };
  51.  
  52. static void PrintInfo();
  53.  
  54. main(argc, argv)
  55.     int argc;
  56.     char **argv;
  57. {
  58.     int spriteID;
  59.     char *myName;
  60.     int virtualHost;
  61.     int physicalHost;
  62.     int status = SUCCESS;
  63.  
  64.     argc = Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), 
  65.              OPT_ALLOW_CLUSTERING);
  66.  
  67.     myName = argv[0];
  68.     
  69.     if (argc > 1) {
  70.     fprintf(stderr, "%s: warning: extra arguments; assuming old format.\n",
  71.         myName);
  72.     getIDs = 1;
  73.     }
  74.     if (machType != -1) {
  75.     Sys_MachineInfo    info;
  76.  
  77.     status = Sys_GetMachineInfo(sizeof (info), &info);
  78.     switch (info.architecture) {
  79.     case SYS_SUN2:
  80.         printf("sun2\n");
  81.         break;
  82.     case SYS_SUN3:
  83.         switch (info.type) {
  84.         case SYS_SUN_3_75:
  85.         printf("sun3/75\n");
  86.         break;
  87. /*
  88.         Duplicate to 3/75!
  89.         case SYS_SUN_3_160:
  90.         printf("sun3/160\n");
  91.         break;
  92. */
  93.         case SYS_SUN_3_50:
  94.         printf("sun3/50\n");
  95.         break;
  96.         case SYS_SUN_3_60:
  97.         printf("sun3/60\n");
  98.         break;
  99.         default:
  100.         printf("sun3\n");
  101.         break;
  102.         }
  103.         break;
  104.     case SYS_SUN4:
  105.         switch (info.type & SYS_SUN_ARCH_MASK) {
  106.         case SYS_SUN_4_C:
  107.         printf("sun4c\n");
  108.         break;
  109.         default:
  110.         printf("sun4\n");
  111.         break;
  112.         }
  113.         break;
  114.     case SYS_DS3100:
  115.         printf("ds3100\n");
  116.         break;
  117.     case SYS_SYM:
  118.         printf("symmetry\n");
  119.         break;
  120.     case SYS_MICROVAX_2:
  121.         printf("microvax2\n");
  122.         break;
  123.     case SYS_SPUR:
  124.         printf("spur\n");
  125.         break;
  126.     case SYS_DS5000:
  127.         printf("ds5000\n");
  128.         break;
  129.     default:
  130.         printf("unknown\n");
  131.         status = 1;
  132.         break;
  133.     }
  134.     exit(status);
  135.     }
  136.  
  137.     if (!virtual & !physical) {
  138.     virtual = 1;
  139.     }
  140.     
  141.     status = Proc_GetHostIDs(&virtualHost, &physicalHost);
  142.     if (status != SUCCESS) {
  143.     errno = Compat_MapCode(status);
  144.     perror("Proc_GetHostIDs");
  145.     exit(1);
  146.     }
  147.     if (virtual) {
  148.     PrintInfo(virtualHost, physical ? "virtual" : (char *) NULL);
  149.     }
  150.     if (physical) {
  151.     PrintInfo(physicalHost, virtual ? "physical" : (char *) NULL);
  152.     }
  153.     exit(0);
  154. }
  155.  
  156.  
  157.  
  158. /*
  159.  *----------------------------------------------------------------------
  160.  *
  161.  * PrintInfo --
  162.  *
  163.  *    Print the information contained in a Host_Entry structure, as
  164.  *    specified by command line arguments, and with any other string
  165.  *     specified by the caller.
  166.  *
  167.  * Results:
  168.  *    None.
  169.  *
  170.  * Side effects:
  171.  *    Data is written to stdout.
  172.  *
  173.  *----------------------------------------------------------------------
  174.  */
  175.  
  176. static void
  177. PrintInfo(spriteID, prefix)
  178.     int spriteID;
  179.     char *prefix;
  180. {
  181.     extern int errno;
  182.     Host_Entry *hostPtr;
  183.  
  184.     hostPtr = Host_ByID(spriteID);
  185.     if (hostPtr == (Host_Entry *) NULL) {
  186.     perror("Host_ByID");
  187.     exit(1);
  188.     }
  189.     if (prefix) {
  190.     printf("%s: ", prefix);
  191.     }
  192.     if (getIDs) {
  193.     printf("%d\n", hostPtr->id);
  194.     } else {
  195.     printf("%s\n", hostPtr->name);
  196.     }
  197. }
  198.